package com.tbit.uqbike.webmanager.controller;

import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.object.pojo.BatteryModelDetail;
import com.tbit.uqbike.object.pojo.MachineBatteryModel;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.ErrorDataList;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.BatteryModelDetailService;
import com.tbit.uqbike.service.business.MachineBatteryModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 电池方案
 *
 * @author Leon
 * 2017年2月24日 下午5:51:05
 */
@Validated
@RestController
@RequestMapping(value = "/machineBatteryModel", produces = BaseController.MEDIA_JSON_UTF8)
public class MachineBatteryModelController extends BaseController {
    @Autowired
    private MachineBatteryModelService machineBatteryModelService;
    @Autowired
    private BatteryModelDetailService batteryModelDetailService;

    /**
     * 批量绑定电池方案/车辆分组
     *
     * @param batteryModelId 电池方案id
     * @param secondaryBattertId 第二电池方案id
     * @param machineNOs 设备编号列表，逗号分隔
     * @param machineGroupId 车辆分组id
     */
    @RequestMapping("/bound")
    @OperateLog(module = ModuleEnum.BATTERYMODEL, methods = MethodEnum.BIND_BATTERYMODEL_GROUP)
    public JsonResult add(Integer batteryModelId,
                          Integer secondaryBattertId,
                          @NotNull(message = "ver.machineNOs.notNull") String machineNOs,
                          Integer machineGroupId,
                          @NotNull(message = "ver.areaId.notNull") Integer accountId,
                          HttpServletRequest request) {
        if (batteryModelId == null && machineGroupId == null) {
            return JsonResult.fail("请选择一项信息进行修改", TraceUtil.getTraceIdByLocal());
        }

        LoginInfo loginInfo = getLogin(request);
        List<String> machineNOList = Arrays.asList(machineNOs.split(","));
        List<ErrorDataList> errDataLists = machineBatteryModelService.
                bound(loginInfo, accountId, batteryModelId, secondaryBattertId, machineNOList, machineGroupId);

        return JsonResult.ok(errDataLists);
    }

    //可以填写设备编号和车辆编号
    @RequestMapping("/boundNew")
    public JsonResult boundNew(@NotNull(message = "ver.batteryModelId.notNull") Integer batteryModelId,
                          Integer secondaryBattertId,
                          @NotNull(message = "ver.machineNOs.notNull") String machineNOs,
                          Integer dataType,
                          HttpServletRequest request) {

        LoginInfo loginInfo = getLogin(request);
        List<String> machineNOList = Arrays.asList(machineNOs.split(","));
        List<ErrorDataList> errDataLists = machineBatteryModelService.
                boundNew(loginInfo, batteryModelId, secondaryBattertId, machineNOList,dataType);
        return JsonResult.ok(errDataLists);
    }

    /* 根据设备编号查询电池方案 */
    @RequestMapping("/getByMachineNO")
    public JsonResult getByMachineNO(@NotNull(message = "ver.machineNO.notNull") String machineNO,
                                     @RequestParam(defaultValue = "10") Integer rowCount,
                                     @RequestParam(defaultValue = "1") Integer pageNO,
                                     HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        MachineBatteryModel machineBatteryModel = machineBatteryModelService.
                getByMachineNO(loginInfo, machineNO);

        Integer total = batteryModelDetailService.getTotal(machineBatteryModel.getBatteryModelId());
        List<BatteryModelDetail> batteryModelDetails = batteryModelDetailService.
                getByBatteryModelId(machineBatteryModel.getBatteryModelId(), rowCount, pageNO);

        return JsonResult.ok(batteryModelDetails, total);
    }

    /* 根据电池方案id查询 */
    @RequestMapping("/getByBatteryModelId")
    public JsonResult getByBatteryModelId(@NotNull(message = "ver.batteryModelId.notNull") Integer batteryModelId,
                                          @RequestParam(defaultValue = "10") Integer rowCount,
                                          @RequestParam(defaultValue = "1") Integer pageNO,
                                          HttpServletRequest request) {
        LoginInfo loginInfo = getLogin(request);
        List<MachineBatteryModel> machineBatteryModels = machineBatteryModelService.
                getByBatteryModelId(loginInfo, batteryModelId, rowCount, pageNO);
        Integer total = machineBatteryModelService.
                getTotalByBatteryModelId(loginInfo, batteryModelId);

        return JsonResult.ok(machineBatteryModels, total);
    }

//
//    /* 根据设备id删除电池绑定方案 */
//    @RequestMapping("/delByMachineId")
//    public void delByMachineId(Integer machineId, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
//        Map<String, Object> map = new HashMap<String, Object>();
//
//        try {
//            Assert.notNull(machineId, "machineId不能为空");
//
//            Account account = (Account) session.getAttribute("account");
//            if (!account.getType().equals(UserConstant.USER_AGENT)) {
//                throw new BaseException("无权操作");
//            }
//
//            /**查询设备*/
//            Machine machine = machineService.getByMachineId(machineId);
//            if (machine == null) {
//                throw new BaseException("设备不存在");
//            }
//
//            if (!machine.getAccountId().equals(account.getAccountId())) {
//                throw new BaseException("无权操作");
//            }
//
//            machineBatteryModelService.delByMachineId(machineId);
//
//            map.put(ResponseCode.RET, ResponseCode.SUCC);
//        } catch (BaseException e) {
//            ResponseCode.printException(map, e);
//        }
//
//        OutputUtil.renderText(request, response, map);
//    }
//
//    /* 根据设备编号批量 */
//    @RequestMapping("/delByMachineNOs")
//    public void delByMachineNOs(String machineNOs, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
//        Map<String, Object> map = new HashMap<String, Object>();
//
//        try {
//            Assert.hasLength(machineNOs, "设备编号不能为空");
//
//            Account account = (Account) session.getAttribute("account");
//            if (!account.getType().equals(UserConstant.USER_AGENT)) {
//                throw new BaseException("无权操作");
//            }
//
//            List<String> machineNOList = Arrays.asList(machineNOs.split(","));
//
//            machineBatteryModelService.delByMachineNOBatch(account.getAccountId(), machineNOList);
//
//            map.put(ResponseCode.RET, ResponseCode.SUCC);
//        } catch (BaseException e) {
//            ResponseCode.printException(map, e);
//        }
//
//        OutputUtil.renderText(request, response, map);
//    }
}